package com.iwhalecloud.bss.kite.cucc.order.accept.chain.impl;

import com.iwhalecloud.bss.kite.cucc.order.accept.chain.AbstractOrderHandler;
import com.iwhalecloud.bss.kite.cucc.order.event.AfterOrderCommitEvent;
import com.iwhalecloud.bss.kite.cucc.order.event.BeforeOrderCommitEvent;
import com.iwhalecloud.bss.kite.order.client.api.operate.IOrderFlowOperateService;
import com.iwhalecloud.bss.kite.order.client.dto.OrderRequest;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * <Description> <br>
 * 订单受理流程：订单提交
 * 
 * @author xiao.xuzhang<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2020年2月12日 <br>
 * @since R9.0<br>
 * @see com.iwhalecloud.bss.kite.cucc.order.accept.chain.impl <br>
 */
@Component
public class OrderCommitHandler extends AbstractOrderHandler {

    @Resource
    private ApplicationContext applicationContext;

    @Resource
    private IOrderFlowOperateService orderFlowOperateService;

    @Override
    public boolean canHandle(OrderRequest orderRequest) {
        if (Objects.equals(Boolean.TRUE, orderRequest.getPreCheck())) {
            // 订单预校验流程不创建订单流程
            return false;
        }

        return true;
    }

    @Override
    public void doHandle(OrderRequest orderRequest) {
        // 订单提交前埋点
        BeforeOrderCommitEvent beforeOrderCommitEvent = new BeforeOrderCommitEvent(this, orderRequest);
        applicationContext.publishEvent(beforeOrderCommitEvent);

        // 订单提交创建订单流程
        orderFlowOperateService.createOrderFlow(orderRequest.getCustOrderId(), orderRequest.getCurrentCustId());

        // 订单提交后埋点
        AfterOrderCommitEvent afterOrderCommitEvent = new AfterOrderCommitEvent(this, orderRequest);
        applicationContext.publishEvent(afterOrderCommitEvent);
    }
}
